package pl.smath.regex;

import java.util.Vector;

import pl.smath.expression.model.Term;
import pl.smath.regex.exceptions.NoMatchFound;
import pl.smath.regex.exceptions.ParseError;
import pl.smath.regex.tree.Parser;
import pl.smath.regex.tree.vertexes.Vertex;

public class Pattern {

	private Vector<Vertex> options = new Vector<Vertex>();
	private Parser parser = new Parser();
	
	public Pattern(Vector<String> options) {
		for (String string : options) {
			try {
				this.options.add(parser.parse(string));
			}
			catch (ParseError e) {
				System.err.println("FAILED TO PARSE OPTION: '" + string + "'");
			}
		}
	}
	

	public Expression process(Term term) throws NoMatchFound {
		Matcher matcher = new Matcher();
		Expression exp;
		for (Vertex check : options) {
			if ((exp = matcher.match(term, check)) != null) 
				return exp;
		}
		throw new NoMatchFound();
	}

	
	public String serialize() {
		String serial = "";
		for (Vertex option : options) {
			serial += option.serialize() + ";";
		}
		return serial;
	}
}
